<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Jobs\EstateRoleJob;
use App\Models\EstateRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @param Request $request
     * @param EstateRole $role_model
     * @return array
     */
    public function index(Request $request, EstateRole $role_model)
    {
        $user = $request->user();
        $list = $role_model->where('estate_id', $user['estate_id'])->orderBy('id', 'desc')->paginate($request->per_page ?? 30);
        return $this->success($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     */
    public function store(Request $request, EstateRole $role_model)
    {
        $user = $request->user();
        $menu = $request->menu_id ? $request->menu_id : [];
        $permission_id = $request->permission_id ? $request->permission_id : [];
        $menus = [];
        //拼装菜单
        foreach ($menu as $v) {
            $menus[$v] = ['estate_id' => $user->estate_id];
        }
        //拼装权限
        $permissions = [];
        foreach ($permission_id as $vs) {
            $permissions[$vs] = ['estate_id' => $user->estate_id];
        }
        $role_model = $role_model->create(['name' => $request->name, 'estate_id' => $user->estate_id]);
        $role_model->menus()->sync($menus ?? []);
        $role_model->permissions()->sync($permissions ?? []);
        return $this->success([], __('base.success'));
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     */
    public function show(EstateRole $role_model, $id)
    {
        $info = $role_model->with(['menus', 'permissions'])->find($id);
        return $this->success($info);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     */
    public function update(Request $request, EstateRole $role_model, $id)
    {

        try {
            $user = $request->user();
            $menu = $request->menu_id ? $request->menu_id : [];
            $permission_id = $request->permission_id ? $request->permission_id : [];
            $role_model = $role_model->find($id);
            $role_model->name = $request->name;
            $role_model->estate_id = $user->estate_id;
            $role_model->save();
            $menus = [];
            //拼装菜单
            foreach ($menu as $v) {
                $menus[$v] = ['estate_id' => $user->estate_id];
            }
            //拼装权限
            $permissions = [];
            foreach ($permission_id as $vs) {
                $permissions[$vs] = ['estate_id' => $user->estate_id];
            }
            $data = [
                'menus' => $menus,
                'permissions' => $permissions,
                'id' => $id
            ];
            // 添加队列
            EstateRoleJob::dispatch($data)->onQueue('EstateRoleJob');
            return $this->success([], __('base.success'));
        } catch (\Exception $e) {
            return $this->error([], $e->getMessage());
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     */
    public function destroy(EstateRole $role_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        foreach ($idArray as $v) {
            $role_model = $role_model->find($v);
            $role_model->menus()->detach();
            $role_model->permissions()->detach();
            $role_model->refresh();
        }
        $role_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }
}
